package com.iris.live.services.report.sources;

import com.google.common.collect.Maps;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.BaseReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author: luojie
 * date: 2017/8/24 17:19
 */
@Component("FinDefeatedResonRptDataSource")
public class FinDefeatedResonRptDataSource  extends BaseReportDataSource{

    @Override
    public List getData(ReportContext context, List<ReportConfig> config) throws Exception {
        Map info= context.getParams() ;
        String lineType=info.get("displayType").toString();
        List<Map> datas =new ArrayList<>();
        if("1".equals(lineType)){
            datas=  getTimeLineData(info);
        }
        else if("2".equals(lineType)){
            datas=  getOrgLineData(info);
        }else {
            datas=getBaseLineData(info);
        }
        int a=datas.size();
        //super.brandGroup(context ,datas);
        return  datas;
    }

    private List getTimeLineData(Map info) {
        String sql = " SELECT DATE_FORMAT(a.开票时间, '%m') AS date, " +
                     "        COUNT(DATE_FORMAT(a.开票时间, '%m') ) AS 数量, " +
                     "        b.`品牌`,  " +
                     "        c.战败原因, " +
                     "        b.级别, " +
                     "        b.车型 " +
                     " FROM vw_group_report_fin_defeat_info a  JOIN lsh车型 b ON a.`车型编号` = b.车型编号 " +
                     "                                         JOIN lsh经销商表 allset ON a.`许可` = allset.`许可_BMBS` " +
                     "                                         JOIN vw_group_report_fin_defeat_base c ON a.战败原因=c.金融战败原因编号" +
                     " WHERE a.`开票时间`>=:startDay AND a.`开票时间`<=:endDay " +
                     " AND 1=1 AND 2=2 " +
                     " GROUP BY date,c.战败原因 " ;
        Map map = Maps.newHashMap();

        String permit = info.get("permit") == null ?"":info.get("permit").toString();
        if(! StringUtils.isNullOrBlank(permit) && !"LSH".equalsIgnoreCase(permit)){
            sql= sql.replace("1=1"," ( allset.大区=:permit  or  allset.大区_EN=:permit or " +
                    "    allset.小区=:permit or allset.小区_EN=:permit or  allset.CTA=:permit or  allset.CTA_EN=:permit    or 小区_EN=:permit or allset.许可_BMBS=:permit )");
            map.put("permit", permit);
        }

        String selBrandname = info.get("selBrandName") == null
                || "all".equals(info.get("selBrandName").toString())
                || "全部".equals(info.get("selBrandName").toString()) ? "":info.get("selBrandName").toString();
        String selCLASS = info.get("selCLASS") == null
                || "all".equals(info.get("selCLASS").toString())
                || "全部".equals(info.get("selCLASS").toString()) ? "":info.get("selCLASS").toString();
        String selType = info.get("selType") == null
                || "all".equals(info.get("selType").toString())
                || "全部".equals(info.get("selType").toString()) ? "":info.get("selType").toString();

        if(StringUtils.isNotNullOrBlank(selBrandname) && StringUtils.isNullOrBlank(selCLASS)){ //品牌 级别 车型
            sql= sql.replace("2=2","  b.品牌=:brandName ");
            map.put("brandName", selBrandname);
        }

        if(StringUtils.isNotNullOrBlank(selCLASS) && StringUtils.isNullOrBlank(selType)){
            sql= sql.replace("2=2","  b.品牌=:brandName AND b.级别=:selCLASS ");
            map.put("brandName", selBrandname);
            map.put("selCLASS", selCLASS);
        }

        if(StringUtils.isNotNullOrBlank(selType)){
            sql= sql.replace("2=2","  b.品牌=:brandName AND b.级别=:selClass AND b.车型=:type ");
            map.put("brandName", selBrandname);
            map.put("selClass", selCLASS);
            map.put("type", selType);
        }
        map.put("startDay",info.get("startDay"));
        map.put("endDay",info.get("endDay"));
        return this.getList(sql,map);
    }

    private List getOrgLineData(Map info){
        String sql = " select " +
                "                'DCH' 集团,allset.许可_LSH,allset.大区 AS 大区, allset.大区_EN AS 大区_EN, allset.小区 AS 小区, allset.小区_EN AS 小区_EN, " +
                "                IF ( isnull(allset.CTA) OR allset.CTA = '', '其他cta', allset.CTA ) AS CTA, " +
                "                IF (isnull(allset.CTA_EN) OR allset.CTA_EN = '', 'Other CTA', allset.CTA_EN ) AS CTA_EN, " +
                "                allset.许可_BMBS AS 许可_BMBS, allset.经销商名 AS 经销商名,allset.经销商名_EN AS 经销商名_EN, allset.SalesCode AS salesCode, " +
                "        COUNT(b.`品牌`) AS 数量, " +
                "        b.`品牌`, " +
                "        c.战败原因" +
                " FROM vw_group_report_fin_defeat_info a JOIN lsh车型 b ON a.`车型编号` = b.车型编号 " +
                "                                        JOIN lsh经销商表 allset ON a.`许可` = allset.`许可_BMBS` " +
                "                                        JOIN vw_group_report_fin_defeat_base c ON a.战败原因=c.金融战败原因编号" +
                " WHERE a.`开票时间`>=:startDay AND a.`开票时间`<=:endDay " +
                " AND 1=1  AND 2=2 " +
                " GROUP BY c.战败原因 ,许可_BMBS" ;
        Map map = Maps.newHashMap();

        String permit = info.get("permit") == null ?"":info.get("permit").toString();
        if(! StringUtils.isNullOrBlank(permit) && !"LSH".equalsIgnoreCase(permit)){
            sql= sql.replace("1=1"," ( allset.大区=:permit  or  allset.大区_EN=:permit or " +
                    "    allset.小区=:permit or allset.小区_EN=:permit or  allset.CTA=:permit or  allset.CTA_EN=:permit    or 小区_EN=:permit or allset.许可_BMBS=:permit )");
            map.put("permit", permit);
        }

        String selBrandname = info.get("selBrandName") == null
                || "all".equals(info.get("selBrandName").toString())
                || "全部".equals(info.get("selBrandName").toString()) ? "":info.get("selBrandName").toString();
        String selCLASS = info.get("selCLASS") == null
                || "all".equals(info.get("selCLASS").toString())
                || "全部".equals(info.get("selCLASS").toString()) ? "":info.get("selCLASS").toString();
        String selType = info.get("selType") == null
                || "all".equals(info.get("selType").toString())
                || "全部".equals(info.get("selType").toString()) ? "":info.get("selType").toString();

        if(!StringUtils.isNullOrBlank(selBrandname) && !"all".equals(selBrandname) && !"全部".equals(selBrandname) && StringUtils.isNullOrBlank(selCLASS)){ //品牌 级别 车型
            sql= sql.replace("2=2","  b.品牌=:brandName ");
            map.put("brandName", selBrandname);
        }

        if(!StringUtils.isNullOrBlank(selCLASS) && !"all".equals(selCLASS) && !"全部".equals(selCLASS) && StringUtils.isNullOrBlank(selType)){
            sql= sql.replace("2=2","  b.品牌=:brandName AND b.级别=:selCLASS ");
            map.put("brandName", selBrandname);
            map.put("selCLASS", selCLASS);
        }

        if(!StringUtils.isNullOrBlank(selType) && !"all".equals(selType) && !"全部".equals(selType)){
            sql= sql.replace("2=2","  b.品牌=:brandName AND b.级别=:selClass AND b.车型=:type ");
            map.put("brandName", selBrandname);
            map.put("selClass", selCLASS);
            map.put("type", selType);
        }

        map.put("startDay",info.get("startDay"));
        map.put("endDay",info.get("endDay"));
        return this.getList(sql,map);
    }

    private List getBaseLineData(Map info){
        String sql=" SELECT DATE_FORMAT(a.开票时间, '%m') AS date, " +
                "        COUNT(b.`品牌`) AS 数量, " +
                "        b.`品牌`,  " +
                "        c.战败原因, " +
                "        b.级别, " +
                "        b.车型 " +
                " FROM vw_group_report_fin_defeat_info a  JOIN lsh车型 b ON a.`车型编号` = b.车型编号 " +
                "                                         JOIN lsh经销商表 allset ON a.`许可` = allset.`许可_BMBS` " +
                "                                         JOIN vw_group_report_fin_defeat_base c ON a.战败原因=c.金融战败原因编号" +
                " WHERE a.`开票时间`>=:startDay AND a.`开票时间`<=:endDay " +
                " AND 1=1 AND 2=2 " +
                " GROUP BY b.品牌,c.战败原因 " ;
        Map map=Maps.newHashMap();

        String permit = info.get("permit") == null ?"":info.get("permit").toString();
        if(! StringUtils.isNullOrBlank(permit) && !"LSH".equalsIgnoreCase(permit)){
            sql= sql.replace("1=1"," ( allset.大区= :permit  or  allset.大区_EN=:permit or " +
                    "    allset.小区=:permit or allset.小区_EN=:permit or  allset.CTA=:permit or  allset.CTA_EN=:permit    or 小区_EN=:permit or allset.许可_BMBS=:permit )");
            map.put("permit", permit);
        }

        String selBrandName = info.get("selBrandName") == null
                || "all".equals(info.get("selBrandName").toString())
                || "全部".equals(info.get("selBrandName").toString()) ? "":info.get("selBrandName").toString();
        if(StringUtils.isNotNullOrBlank(selBrandName) && !"all".equals(selBrandName) && !"全部".equals(selBrandName)){ //品牌 级别 车型
            sql= sql.replace("2=2","  b.品牌=:brandName ");
            map.put("brandName", selBrandName);
        }

        map.put("startDay",info.get("startDay"));
        map.put("endDay",info.get("endDay"));
        return this.getList(sql,map);
    }
}